package com.yy.train.ticket.utils;

import org.springframework.format.datetime.DateFormatter;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @author yy1
 * @date 2021年11月10日 14:54
 */
public class LocalDateTimeUtils {

    /**
     * 英文全称 如：2010-12-01 23:15:06
     */
    public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static String yyyy_MM_dd = "yyyy-MM-dd";

    public static String HH_mm_ss = "HH:mm:ss";

    /**
     * 1.将LocalDateTime转为自定义的时间格式的字符串
     * @param localDateTime
     * @param format
     * @return
     */
    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 当前时间转换成 yyyyMMddHHmmss
     * @return
     */
    public static String formatterToyyyyMMddHHmmss() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMddHHmmss);
        return localDateTime.format(formatter);
    }
    public static String formatterToyyyyMMddHHmmssSSSsnow() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMddHHmmssSSS);
        return localDateTime.format(formatter);
    }

    /**
     * 当前时间转换成 yyyyMMddHHmmss
     * @return
     */
    public static String formatterToyyyy_MM_dd_HH_mm_ss() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyy_MM_dd_HH_mm_ss);
        return localDateTime.format(formatter);
    }
    public static LocalDate formatterToyyyy_MM_dd(String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyy_MM_dd);
        return LocalDate.parse(time, formatter);
    }
    public static LocalTime formatterToHH_mm_ss(String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(HH_mm_ss);
        return LocalTime.parse(time, formatter);
    }
    /**
     * 2.将long类型的timestamp转为LocalDateTime
     * @param timestamp
     * @return
     */

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * 3.将LocalDateTime转为long类型的timestamp
     * @param localDateTime
     * @return
     */
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 4.将某时间字符串转为自定义时间格式的LocalDateTime
     * @param time
     * @param format
     * @return
     */

    public static LocalDateTime parseStringToLocalDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }
    public static LocalDateTime yyyyMMddHHmmssToLocalDateTime(String time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(yyyyMMddHHmmss);
        return LocalDateTime.parse(time, df);
    }
    /**
     * 判断开始时间是否大于结束时间
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isBefore( LocalDateTime startTime, LocalDateTime endTime) {
        return startTime.isBefore(endTime);//你的时间在当前时间之前是true
    }
    /**
     * 判断开始时间是否小于结束时间
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isAfter( LocalDateTime startTime, LocalDateTime endTime) {
        return startTime.isAfter(endTime);
    }

    public static void main(String[] args) {
       /* String time1 = "2019-06-26 19:00:00";
        String time2 = "2019-07-26 19:00:00";
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(time1, dtf2);
        LocalDateTime endTime = LocalDateTime.parse(time2, dtf2);
        System.out.print(isBefore(  startTime,  endTime));*/
        isAfter1();
    }
    public static void isAfter1() {
        LocalDateTime payTime = LocalDateTime.now();
        DateTimeFormatter formatter =  DateTimeFormatter.ofPattern("yyyyMMdd");
        String chargeYear = formatter.format(payTime);
        System.out.println(chargeYear);
    }

}
